package com.example.bbs.controller;

import com.example.bbs.service.CommentService;
import com.example.bbs.util.CommentResolve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/7/5 11:24
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    private Map<String, String> supportMap;

    public List<CommentResolve> commentsOfLevel1(int artid) {
        return commentService.getCommentsOfLevel1(artid);
    }

    @RequestMapping("/getcomment2_3/{artid}/{comment1id}")
    @ResponseBody
    public List<CommentResolve> commentsOfLevel2_3(@PathVariable("artid") int artid, @PathVariable("comment1id") int comment1id) {
        return commentService.getCommentsOfLevel2_3(artid, comment1id);
    }

    @RequestMapping("addcomment1")
    @ResponseBody
    public String addcomment1(@RequestBody Map<String, String> paramMap) {
        try {
            int artid = Integer.parseInt(paramMap.get("artid"));
            int commentatorid = Integer.parseInt(paramMap.get("commentatorid"));
            String content = paramMap.get("content");
            return commentService.addComment1(artid, commentatorid, content);
        } catch (Exception e) {
            return "500";
        }
    }

    @RequestMapping("addcomment2")
    @ResponseBody
    public String addcomment2(@RequestBody Map<String, String> paramMap) {
        try {
            int artid = Integer.parseInt(paramMap.get("artid"));
            int commentatorid = Integer.parseInt(paramMap.get("commentatorid"));
            int comment1id = Integer.parseInt(paramMap.get("comment1id"));
            String content = paramMap.get("content");
            return commentService.addComment2(artid, commentatorid, comment1id, content);
        } catch (Exception e) {
            return "500";
        }
    }

    @RequestMapping("addcomment3")
    @ResponseBody
    public String addcomment3(@RequestBody Map<String, String> paramMap) {
        try {
            int artid = Integer.parseInt(paramMap.get("artid"));
            int commentatorid = Integer.parseInt(paramMap.get("commentatorid"));
            int comment1id = Integer.parseInt(paramMap.get("comment1id"));
            int comment2id = Integer.parseInt(paramMap.get("comment2id"));
            String content = paramMap.get("content");
            return commentService.addComment3(artid, commentatorid, comment1id, comment2id, content);
        } catch (Exception e) {
            return "500";
        }
    }

    @RequestMapping("/updatesupport")
    @ResponseBody
    public String updateSupport(@RequestBody Map<String, String> paramMap) {
        try {
            int commentid = Integer.parseInt(paramMap.get("commentid"));
            int value = Integer.parseInt(paramMap.get("value"));
            return commentService.updateSupport(commentid,value);
        } catch (Exception e) {
            return "error";
        }
    }


}
